package com.liuyi.netty.mqtt.processed.process.impl;

import com.liuyi.netty.mqtt.configution.MqttBeanManage;
import com.liuyi.netty.mqtt.processed.process.MqttProcess;
import com.liuyi.netty.mqtt.processed.store.MqttChannelManage;
import com.liuyi.netty.mqtt.processed.store.MqttSessionStore;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.mqtt.MqttMessage;
import lombok.extern.slf4j.Slf4j;

/**
 * 客户端关闭连接
 *
 * @author Mr.Fmy
 * @since 2021-09-28
 */
@Slf4j
public class DisconnectMqttProcess implements MqttProcess {

    private final MqttBeanManage beanManage;

    private MqttSessionStore sessionStore;

    public DisconnectMqttProcess(MqttBeanManage beanManage) {
        this.beanManage = beanManage;
        this.sessionStore = this.beanManage.getMqttSessionStore();
    }

    @Override
    public void process(ChannelHandlerContext channel, MqttMessage msg) {
        String clientId = MqttChannelManage.getClientId(channel);
        MqttChannelManage.close(clientId);
        this.sessionStore.offlineSession(clientId);
        log.info("Current Mqtt Connect Size：{}", this.sessionStore.count());
    }
}
